begintownscript;

variables;
short message_lost,night_time;

body;

beginstate INIT_STATE;
// This state called whenever this section is loaded into memory.
	set_name(9,"Father Kernighan");	// Name Kernighan and give him a pic
	set_char_dialogue_pic(9,1961,0);
	set_name(11,"Ritchie");		// Likewise for the Innkeep
	set_char_dialogue_pic(11,1948,0);
	set_name(7,"Lenore");		// And Lenore
	set_char_dialogue_pic(7,1959,0);
	set_name(8,"Zak");		// Likewise for the assassin
	set_char_dialogue_pic(8,1945,0);
	set_name(16,"Grace");		// Likewise for Grace Hopper
	set_char_dialogue_pic(16,1947,0);
	add_char_to_group(9,1);		// the three tavern guests are one group
	add_char_to_group(8,1);
	add_char_to_group(7,1);
	add_char_to_group(10,2);  // sendings
	add_char_to_group(19,2);
	add_char_to_group(20,2);
	add_char_to_group(21,2);
	if (get_flag(200,1)>0) // if they got robbed
	{
		set_creature_memory_cell(11,3,23); // change ritchie's dialogue.
	}
	if (get_flag(2,6)==1 && get_flag(0,7)<2)
	{
		activate_hidden_group(1);
		set_name(14,"Father Kernighan");
		set_char_dialogue_pic(14,1961,0);
	}
break;
beginstate START_STATE;
	message_lost = get_flag(200,1);
	night_time   = get_flag(200,2);
	// this needs to be here in the start state to simulate a one-move delay between 
	if (message_lost==0 && get_flag(0,3)==1) set_state_continue(12);
// Starting state of the section, called every turn the party 
// stands inside this section until you change the state.
	if (get_current_tick()%5000>500 && get_current_tick()%5000<4500)
	{
		activate_hidden_group(2);
		set_name(16,"Grace");
		set_char_dialogue_pic(16,1947,0);		
	}
	if (get_flag(0,7)==0 && dist_to_waypoint(14,2)==0) 
	{
		message_dialog("You see Kernighan standing near the window of the inn, staring right at you. He quickly moves away from the window and sits down.","");
		approach_waypoint(14,3,0);
	}
	if (get_flag(0,7)==0 && dist_to_waypoint(14,3)==0 && get_flag(0,7)==0)
	{
		set_mobility(14,0); // freeze!
	}

	if (get_flag(0,7)==1)
	{
		set_character_pose(14,2);
		force_instant_terrain_redraw();
		activate_hidden_group(3);
		force_instant_terrain_redraw();
		put_effect_on_char(10,10,5,0);
		put_effect_on_char(19,10,5,0);
		put_effect_on_char(20,10,5,0);
		put_effect_on_char(21,10,5,0);
		put_jagged_zap(11,8,char_loc_x(10),char_loc_y(10),3);
		put_jagged_zap(11,8,char_loc_x(19),char_loc_y(19),3);
		put_jagged_zap(11,8,char_loc_x(20),char_loc_y(20),3);
		put_jagged_zap(11,8,char_loc_x(21),char_loc_y(21),3);
		play_sound(-62);
		run_animation();
		set_attitude(14,10);
		set_mobility(14,1);
		message_dialog("Ritchie reveals himself as less of a hero than he implied earlier on. Surely a man like Claw Hardblade would not have run from the room and hidden himself like this, screaming for help?","");
		while (char_loc_y(11)<10)
		{
			relocate_character(11,char_loc_x(11),char_loc_y(11)+1);
			force_instant_terrain_redraw();
			pause(3);
		}
		while (char_loc_x(11)<9)
		{
			relocate_character(11,char_loc_x(11)+1,char_loc_y(11)+1);
			erase_text_bubbles();
			if (char_loc_x(11)<7) text_bubble_on_char(11,"Help!");
			else text_bubble_on_char(11,"HELP!");
			force_instant_terrain_redraw();
			pause(3);
		}
		erase_char(11);
		force_instant_terrain_redraw();
		set_flag(0,7,2);
	}
break;

beginstate EXIT_STATE;
break;

beginstate 10; // try to leave
	if (message_lost==0) {
		message_dialog("You have ridden hard today and made good progress. Your horses are protesting at the idea of pressing onward through the night, and you feel tired as well.","That inn looks kind of cozy...");
		block_entry(1);
	}
break;

beginstate 11; // enter inn
	if (get_flag(0,1)==1) end();
	message_dialog("You enter the tavern, which the sign outside proclaims to be the Thirsty Herald. Given your parched throats and your current profession as messengers, you find it oddly suitable.",
		"The inn looks as cozy inside as out. Three other guests are gathered around the only table, but only by necessity. None of them are talking to each other.");
	set_flag(0,1,1);
break;

beginstate 12; 
	if (get_flag(200,1)==1) end();
	set_flag(200,1,1); // steal that message.
	change_spec_item(0,-1); // take it away...
	toggle_quest(1,1); // and charge the player with retrieving it.
	create_horse(1,-1,-1,-1,1); // remove father kernighan's horse
	erase_char(1001); // and kernighan himself (and company)
	set_event_happened(0,0); // and never make them return
	set_flag(0,2,1); // and take his carriage as well.

	move_item_on_spot(10,9,-1,-1); // and clean up the table
	move_item_on_spot(10,8,-1,-1);
	move_item_on_spot(10,7,-1,-1);

//	flip_terrain(31,14); // also, open the door to Hopper. if the player bashed it open, well TOUGH.
				// if I got robbed during the night, I'd keep the door locked throughout the next day, TOO!
	
	// okay, I can't actually do this, it's really awkward. instead:
	set_terrain(31,14,15); //there, that was easy.


	set_flag(0,6,1); // so the door will be unlocked on town entry.

	reset_dialog();
	add_dialog_str(0,"Perhaps it is because of the drink, or the sense of comfort and security that the inn's atmosphere gave you. You did not take turns guarding and sleeping.",0);
	add_dialog_str(1,"Your sleep is wholesome, nearly dreamless and uninterrupted.",0);
	add_dialog_str(2,"You wake in the morning, feeling well-rested, but there is an odd sense of danger tickling the back of your necks.",0);
	add_dialog_str(3,"Finally, you figure out what it is: The door to your room is slightly open. You were sure you locked it well last night.",0);
	add_dialog_str(4,"Hastily, you check for the courier pouch that you placed in the bottom drawer of the cupboard.",0);
	add_dialog_str(5,"It is gone.",0);
	run_dialog(1);
	set_ticks_forward(200); // they slept *long* tonight.
	set_creature_memory_cell(11,3,23); // ritchie updates his dialogue.
	set_state_continue(2); // jump back to our original script.
break;

beginstate 13;
	approach_waypoint(14,2,0);
	//print_str("He's seen you!");
break;

beginstate 14;
	block_entry(1);
	print_str_color("Ritchie would object to you exploring his cellar without permission.",1);
break;

beginstate 15;
	block_entry(1);
	print_str_color("Only paying guests are allowed to the luxury quarters upstairs.",1);
break;
